<?php
if (!isset($_COOKIE["cur_user"])) {
    echo <<<EOF
<script>
alert("You need to log in!");
url="index.php";
window.location.href=url;
</script>
EOF;
} else {
    setcookie("cur_user", $_COOKIE["cur_user"], time() + 3600);
    setcookie("cur_sid", $_COOKIE["cur_sid"], time() + 3600);
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Course Available</title>
    <link rel="stylesheet" href="bootstrap_min.css">

    <style>
        #no-result-warning {
            font-family: Sansation, sans-serif;
            color: gray;
        }

        .hide {
            display: none;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
    <div class="container-fluid">
        <a class="navbar-brand" href="#">
            <span>Registration</span>
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse"
                data-bs-target="#navbarSupportedContent"
                aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                <li class="nav-item">
                    <a class="nav-link" href="stuCourseAvailable.php">Course Available</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link active" href="#">Course Selected</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link disabled">Course Information</a>
                </li>
            </ul>
            <form class="d-flex" method="get" action="stuCourseSelected.php">
                <input class="form-control me-2" type="search" placeholder="Search" name="keyword">
                <button class="btn btn-outline-success me-2" type="submit">Search</button>
            </form>
            <form action="logOut.php" method="post" class="d-flex">
                <button class="btn btn-outline-danger" type="submit">Log Out</button>
            </form>
        </div>
    </div>
</nav>

<section>
    <div class="container">
        <table class="table">
            <thead>
            <tr>
                <th>CID</th>
                <th>Course Name</th>
                <th>Course Time</th>
                <th>Operation</th>
            </tr>
            </thead>
            <tbody id="course-list">
            </tbody>
        </table>

        <h3 class="text-center hide" id="no-result-warning">Nothing Found</h3>
    </div>
</section>
<script src="bootstrap_js.js"></script>
<script>
    function add_course(cid, cname, week_day, period) {
        let target_table = document.getElementById("course-list");
        let new_tr = document.createElement("tr");
        new_tr.id = cid;

        let temp_cid = document.createElement("td");
        temp_cid.classList.add("align-middle");
        let wrapper_cid = document.createElement("div");
        wrapper_cid.innerHTML = cid;
        temp_cid.appendChild(wrapper_cid);
        new_tr.appendChild(temp_cid);  // 烦了，删库跑路了（寻妹亲手写的注释）

        let temp_cname = document.createElement("td");
        temp_cname.classList.add("align-middle");
        let wrapper_cname = document.createElement("div");
        wrapper_cname.innerHTML = cname;
        temp_cname.appendChild(wrapper_cname);
        new_tr.appendChild(temp_cname);

        let temp_ctime = document.createElement("td");
        temp_ctime.classList.add("align-middle");
        let wrapper_ctime = document.createElement("div");
        wrapper_ctime.classList.add("align-middle");
        wrapper_ctime.innerHTML = week_day + ", " + period;
        temp_ctime.appendChild(wrapper_ctime);
        new_tr.appendChild(temp_ctime);

        let wrapper = document.createElement("td");
        let form_view = document.createElement("form");
        form_view.method = "get";
        form_view.action = "courseInfo.php";
        let view_button = document.createElement("button");
        view_button.innerHTML =
            "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
            "view" +
            "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        view_button.type = "submit";
        view_button.classList.add("btn");
        view_button.classList.add("btn-outline-dark");
        view_button.name = "courseID";
        view_button.value = cid;
        form_view.appendChild(view_button);
        wrapper.appendChild(form_view);
        new_tr.appendChild(wrapper);

        let form_choose = document.createElement("form");
        form_choose.method = "get";
        form_choose.action = "courseWithdrawal.php";
        let choose_button = document.createElement("button");
        choose_button.innerHTML = "Withdrawal";
        choose_button.type = "submit";
        choose_button.classList.add("btn");
        choose_button.classList.add("btn-outline-danger");
        choose_button.classList.add("mt-1");
        choose_button.name = "courseID";
        choose_button.value = cid;
        form_choose.appendChild(choose_button);
        wrapper.appendChild(form_choose);
        new_tr.appendChild(wrapper);

        target_table.appendChild(new_tr);
    }
</script>
</body>
</html>

<?php
if (isset($_COOKIE["cur_user"])) {
    $week_name = array(
        "1" => "Monday",
        "2" => "Tuesday",
        "3" => "Wednesday",
        "4" => "Thursday",
        "5" => "Friday",
        "6" => "Saturday",
        "7" => "Sunday",
    );

    $period_name = array(
        "1" => "8:00 - 9:35",
        "2" => "9:55 - 11:30",
        "3" => "13:20 - 14:55",
        "4" => "15:15 - 16:50",
        "5" => "18:30 - 20:55",
    );

    $sid = $_COOKIE["cur_sid"];
    $conn = new PDO("sqlite:storage.db");
    if (isset($_GET["keyword"])) {
        $keyword_filtered = htmlspecialchars(trim(stripslashes($_GET["keyword"])));
        $sql_command = <<<EOF
select * from course where (
    cname like '%$keyword_filtered%'
    and
    cid in (
        select cid from choose where sid=?
    )
)
EOF;
    } else {
        $sql_command = <<<EOF
select * from course where (
    cid in (
        select cid from choose where sid=?
    )
)
EOF;
    }
    $sql_run = $conn->prepare($sql_command);
    $sql_run->execute([$sid,]);
    $conn = null;

    $no_result = true;
    foreach ($sql_run as $row) {
        $temp_cid = $row["cid"];
        $temp_cname = $row["cname"];
        $temp_date = $row["ctime"];
        $td1 = $week_name[substr($temp_date, 0, 1)];
        $td2 = $period_name[substr($temp_date, 2)];
        echo <<<EOF
<script>
add_course("$temp_cid", "$temp_cname", "$td1", "$td2");
console.log("$temp_cname");
</script>
EOF;
        $no_result = false;
    }
    if ($no_result) {
        echo <<<EOF
<script>
document.getElementById("no-result-warning").classList.remove("hide");
</script>
EOF;
    }
}
?>
